# # 七、使用turtle模块结合类封装一下功能，
# # 绘制圆形，绘制矩形，绘制三角形，绘制五角星，绘制爱心
#
# import turtle
#
#
# # turtle.forward(100)
# # turtle.left(90)
# # turtle.forward(100)
# # turtle.left(90)
# # turtle.forward(100)
# # turtle.left(90)
# # turtle.forward(100)
# # turtle.mainloop()
#
# class Draw:
#     def __init__(self, width=5, pencolor="green", fillcolor="pink", bgcolor="grey"):
#         turtle.width(width)
#         turtle.pencolor(pencolor)
#         turtle.fillcolor(fillcolor)
#         turtle.bgcolor(bgcolor)
#
#     def draw_circle(self, x, y, r):
#         turtle.setheading(0)
#         turtle.up()
#         turtle.goto(x, y - r)
#         turtle.down()
#         turtle.circle(r)
#
#     def draw_triangle(self, x, y, x1, y1, x2, y2):
#         turtle.setheading(0)
#         turtle.up()
#         turtle.goto(x, y)
#         turtle.down()
#         turtle.goto(x1, y1)
#         turtle.goto(x2, y2)
#         turtle.goto(x, y)
#
#     def draw_five(self, x, y):
#         turtle.setheading(0)
#         turtle.up()
#         turtle.goto(x, y)
#         turtle.down()
#         for i in range(1, 6):
#             turtle.forward(100)  # 向前100
#             turtle.right(144)  # 顺时针旋转144°
#
#     def run(self):
#         turtle.mainloop()
#
#
# draw = Draw()
#
# # draw.draw_circle(100, 100, 100)
# # draw.draw_circle(-100, 100, 100)
# # draw.draw_circle(-100, -100, 100)
# # draw.draw_circle(100, -100, 100)
#
# # draw.draw_triangle(0, 0, 100, 100, -200, 250)
# # draw.draw_triangle(200, 200, -100, 100, -200, -250)
#
# draw.draw_five(0, 0)
# draw.draw_five(-200, -100)
#
# draw.run()
import tkinter
from tkinter import colorchooser
from tkinter import messagebox


#
# #  一个专门做界面的工具模块
# import tkinter
#
# # print(tkinter)
#
#
# root = tkinter.Tk()
# root.title("我是睡神")
#
# label1 = tkinter.Label(root, text="我就是要睡觉")
# label1.pack()
#
# count = 0
# label2 = tkinter.Label(root, text=f"睡觉次数 {count}")
# label2.pack()
#
#
#
# def click_event():
#     print("怎么也叫不醒")
#     global count
#     count += 1
#
#     label2.config(text=f"睡觉次数 {count}")
#
# button1 = tkinter.Button(root, text="睡觉", command=click_event)
# button1.pack()
#
# root.mainloop()
#
# # 需要自学tikinter 的Canvas(包含事件), Menu,


class Draw:
    def __init__(self, title="画图软件"):
        self.root = tkinter.Tk()
        self.root.title(title)
        self.init_menu()
        self.init_canvas()
        self.init_status()

        # 现在选择图形   现在使用颜色
        self.current_shape = None
        self.current_color = "black"

        # 当前绘制图形的id
        self.max_id = 0
        self.start_x = None
        self.start_y = None

    def set_shape(self, shape):
        self.current_shape = shape
        print(f"当前图形{self.current_shape}")
        if self.current_shape == "line":
            self.status.config(text=f"当前选择的是直线")
        elif self.current_shape == "oval":
            self.status.config(text=f"当前选择的是椭圆")
        elif self.current_shape == "rect":
            self.status.config(text=f"当前选择的是矩形")
        else:
            self.status.config(text=f"请选择图形开始绘制")

    def set_color(self, color):
        self.current_color = color
        print(f"当前选择颜色{self.current_color}")

    def choose_color(self):
        r = colorchooser.askcolor()
        if r[1]:
            self.set_color(r[1])

    def quit(self):
        r = messagebox.askyesno(title="退出", message="是否确定退出程序")
        if r:
            self.root.destroy()

    def undo(self):
        if self.max_id > 0:
            self.canvas.delete(self.max_id)
            self.max_id -= 1

    def clear(self):
        self.canvas.delete()
        self.max_id = 0

    def init_menu(self):
        # 主菜单
        self.main_menu = tkinter.Menu(self.root)
        # 图形菜单
        self.shape_menu = tkinter.Menu(self.main_menu, tearoff=False)
        self.shape_menu.add_command(label="直线", command=lambda: self.set_shape("line"))
        self.shape_menu.add_command(label="椭圆", command=lambda: self.set_shape("oval"))
        self.shape_menu.add_command(label="矩形", command=lambda: self.set_shape("rect"))
        self.main_menu.add_cascade(label="图形", menu=self.shape_menu)

        # 颜色菜单
        self.color_menu = tkinter.Menu(self.main_menu, tearoff=False)
        self.color_menu.add_command(label="红色", command=lambda: self.set_color("red"))
        self.color_menu.add_command(label="绿色", command=lambda: self.set_color("green"))
        self.color_menu.add_command(label="蓝色", command=lambda: self.set_color("blue"))
        self.color_menu.add_separator()
        self.color_menu.add_command(label="自定义", command=self.choose_color)
        self.main_menu.add_cascade(label="颜色", menu=self.color_menu)

        # 其他
        self.other_menu = tkinter.Menu(self.main_menu, tearoff=False)
        self.other_menu.add_command(label="撤销", command=self.undo)
        self.other_menu.add_command(label="清屏", command=self.clear)
        self.other_menu.add_command(label="退出", command=quit)
        self.main_menu.add_cascade(label="其他", menu=self.other_menu)

        self.root.config(menu=self.main_menu)

    def begin_draw(self, e):
        self.start_x = e.x
        self.start_y = e.y
        if self.current_shape == "line":
            self.max_id = self.canvas.create_line(self.start_x, self.start_y, self.start_x, self.start_y,
                                                  fill=self.current_color)
        elif self.current_shape == "oval":
            self.max_id = self.canvas.create_oval(self.start_x, self.start_y, self.start_x, self.start_y,
                                                  outline=self.current_color)
        elif self.current_shape == "rect":
            self.max_id = self.canvas.create_rectangle(self.start_x, self.start_y, self.start_x, self.start_y,
                                                       outline=self.current_color)

    def update_draw(self, e):
        if self.current_shape != "none":
            self.canvas.coords(self.max_id, self.start_x, self.start_y, e.x, e.y)

    def stop_draw(self, e):
        self.start_x = None
        self.start_y = None

    def init_canvas(self):
        self.root.maxsize(width=800, height=630)
        self.canvas = tkinter.Canvas(self.root, width=800, height=600)
        self.canvas.pack()

        # 绑定左键点击  鼠标移动  左键抬起 右键抬起
        self.canvas.bind("<Button-1>", func=self.begin_draw)
        self.canvas.bind("<Motion>", func=self.update_draw)
        self.canvas.bind("<ButtonRelease-1>", func=self.stop_draw)
        self.canvas.bind("<ButtonRelease-3>", lambda e: self.set_shape("none"))

    def init_status(self):
        self.status = tkinter.Label(self.root, text=f"请选择一个图形开始绘制")
        self.status.pack(side="left")

    def run(self):
        self.root.mainloop()


draw = Draw()
draw.run()
